<?php

class sendCronjobMail extends CComponent {

    public function sendMailtoClassExp() {
        $date = date('Y-m-d');
        $viewPath = Yii::getPathOfAlias('application.components.views.mails');
        $contents = ClassBatch::model()->findAll('start_date=' . "'$date'" . ' AND (schedule_id=1 OR schedule_id=4 )');
        if (!empty($contents)) {
            foreach ($contents as $content) {
                $currentBachExp = ClassBatch::model()->find('classes_id=' . $content->classes_id . ' AND start_date>' . "'$date'");
                $currentsession = BatcheSession::model()->find('classes_id=' . $content->classes_id . ' AND date>' . "'$date'");
                $getClaCourseInfo = getClaCourseInfo('Classes', $content->classes_id);
                if (empty($currentBachExp) && empty($currentsession)) {
                    if ($getClaCourseInfo->users_id == '') {
                        $userdetails = Users::getBackEndUser($getClaCourseInfo->backend_user_id);
                        $mailto = $userdetails->user->email;
                    } else {
                        $userdetails = Users::getFrontEndUser($getClaCourseInfo->users_id);
                        $mailto = $userdetails->user->username;
                    }
                    Classes::model()->updateByPk($getClaCourseInfo->id, array('class_exp' => SkillConfig::STATUS_ACTIVE));
                    ShoppingCartByUser::model()->deleteAllByAttributes(array('class_id' => $getClaCourseInfo->id));
                    $userdetails = $userdetails->first_name . ' ' . $userdetails->last_name;
                    $classcoursename = $getClaCourseInfo->class_titile;
                    $mailmodel = EmailTemplate::model()->findByPk(Message::Exp_Notification);
                    $mailbody = array('value' => $mailmodel->body, 'type' => 'raw');
                    $subject = $mailmodel->subject;
                    $arrayFind = array('[username]', '[classcoursename]');
                    $arrayReplace = array($userdetails, $classcoursename);
                    $mailbody = str_replace($arrayFind, $arrayReplace, $mailbody);
                    $mailtemp = Template::model()->findByPk($mailmodel->template_id);
                    $body = CConsoleCommand::renderFile($viewPath . '/' . $mailtemp->filename . '.php', array('mailbody' => $mailbody['value']), true, false);
                    try {
                        SkillSendMail::SendMailCreateCLassCourse($mailto, $subject, $body);
                        /* $this->redirect(array('view', 'id' => $model->id)); */
                    } catch (Exception $e) {
                        echo $e;
                        exit;
                    }
                    return true;
                }
            }
        }
    }

    public function CourseExp() {
        $date = date('Y-m-d');
        $viewPath = Yii::getPathOfAlias('application.components.views.mails');
        $contents = CourseBatche::model()->findAll('date_to=' . "'$date'");
        if (!empty($contents)) {
            foreach ($contents as $content) {
                $currentBachExp = CourseBatche::model()->find('courses_id=' . $content->courses_id . ' AND date_to>' . "'$date'");
                $getClaCourseInfo = getClaCourseInfo('Courses', $content->courses_id);
                if (empty($currentBachExp)) {
                    if ($getClaCourseInfo->users_id == '') {
                        $userdetails = Users::getBackEndUser($getClaCourseInfo->backend_user_id);
                        $mailto = $userdetails->user->email;
                    } else {
                        $userdetails = Users::getFrontEndUser($getClaCourseInfo->users_id);
                        $mailto = $userdetails->user->username;
                    }
                    Courses::model()->updateByPk($getClaCourseInfo->id, array('course_exp' => SkillConfig::STATUS_ACTIVE));
                    $userdetails = $userdetails->first_name . ' ' . $userdetails->last_name;
                    $classcoursename = $getClaCourseInfo->course_titile;
                    $mailmodel = EmailTemplate::model()->findByPk(Message::Exp_Notification);
                    $mailbody = array('value' => $mailmodel->body, 'type' => 'raw');
                    $subject = $mailmodel->subject;
                    $arrayFind = array('[username]', '[classcoursename]');
                    $arrayReplace = array($userdetails, $classcoursename);
                    $mailbody = str_replace($arrayFind, $arrayReplace, $mailbody);
                    $mailtemp = Template::model()->findByPk($mailmodel->template_id);
                    $body = CConsoleCommand::renderFile($viewPath . '/' . $mailtemp->filename . '.php', array('mailbody' => $mailbody['value']), true, false);
                    try {
                        SkillSendMail::SendMailCreateCLassCourse($mailto, $subject, $body);
                        /* $this->redirect(array('view', 'id' => $model->id)); */
                    } catch (Exception $e) {
                        echo $e;
                        exit;
                    }
                    return true;
                }
            }
        }
    }

}

?>
